$(function() {
	$('#dataGrid').datagrid({
		url : 'sensor/dataGrid',
		striped : true,
		rownumbers : true,
		pagination : true,
		singleSelect : true,
		idField : 'id',
		sortName : 'id',
		sortOrder : 'asc',
		pageSize : 50,
		pageList : [ 10, 20, 30, 40, 50, 100, 200, 300, 400, 500 ],
		frozenColumns : [ [ {
			width : '80',
			title : '传感器编号',
			field : 'sensorNum'
		},{
			width : '180',
			title : '传感器名称',
			field : 'sensorName'
		},{
			width : '100',
			title : '传感器类型',
			field : 'typeName'
		},{
			width : '180',
			title : '生产厂家',
			field : 'company'
		},{
			width : '180',
			title : '出厂日期',
			field : 'productDate',
			formatter: function (value, row, index) {
				return formatDate(value);
            }
		},{
			field : 'action',
			title : '操作',
			width : 200,
			formatter : function(value, row, index) {
				var str = '&nbsp;';
				if(row.isdefault!=0){
					if ($.canEdit) {							
						str += $.formatString('<a href="javascript:void(0)" onclick="editSensor(\'{0}\');" >编辑</a>', index);
					}
					if ($.canDelete) {
						str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
						str += $.formatString('<a href="javascript:void(0)" onclick="deleteSensor(\'{0}\');" >删除</a>', row.id);
					}
				}
				return str;
			}
		} ] ],
		toolbar : '#toolbar'
	});
	
	$("#dia-add").dialog({
		title:"添加传感器",
        modal: true,
        width: 500,  
        height: 300,
        closed: true,
        buttons : [{
			text : '添加',
			iconCls : 'icon-add',
			handler : function () {
				var bool=$("#form-add").form('validate');
				if(bool){
					$.ajax({
						cache:false,
						url:'sensor/add',
						data:$("#form-add").serialize(),
						type:"POST",
						dataType:"json",
						success:function(result){
							if(!result.success){
								alert(result.msg);
							}else{
								$("#dataGrid").datagrid('reload');
								$("#dia-add").dialog('close');
							}
						}
					});
				}
			}
		},{
			text : '取消',
			iconCls : 'icon-redo',
			handler : function () {
				$("#dia-add").dialog('close');
			}
		}]
	});
	
	$("#dia-edit").dialog({
		title:"编辑传感器",
        modal: true,
        width: 550,  
        height: 350,
        closed: true,
        buttons : [{
			text : '编辑',
			iconCls : 'icon-edit',
			handler : function () {
				var bool=$("#form-edit").form('validate');
				if(bool){
					$.ajax({
						cache:false,
						url:'sensor/edit',
						data:$("#form-edit").serialize(),
						type:"POST",
						dataType:"json",
						success:function(map){
							if(!map.success){
								alert(map.msg);
							}else{
								$('#dataGrid').datagrid('reload');
								$("#dia-edit").dialog('close');
							}
						}
					});
				}
			}
		},{
			text : '取消',
			iconCls : 'icon-redo',
			handler : function () {
				$("#dia-edit").dialog('close');
			}
		}]
	});
});

function addSensor() {
	initType('#dia-add #s_type');
	$('#dia-add #pid').combobox('select',0);
	$("#dia-add").dialog('open');
}


function editSensor(index) {
	var rows=$('#dataGrid').datagrid('getRows');
	var row=rows[index];
	$("#dia-edit input[name=id]").val(row.id);
	$("#dia-edit input[name=sensorNum]").val(row.sensorNum);
	$("#dia-edit input[name=sensorName]").val(row.sensorName);
	$("#dia-edit input[name=company]").val(row.company);
	$("#dia-edit #date").datebox('setValue',formatDate(row.productDate));
	initType('#dia-edit #s_type');
	$('#dia-edit #s_type').combobox('select',row.sensorType);
	$("#dia-edit").dialog('open');
	
}	

function deleteSensor(id) {
	if(confirm("确定要删除当前传感器吗？")){
		$.ajax({
			cache:false,
			url:"sensor/delete",
			data:{id:id},
			type:"POST",
			dataType:"json",
			success:function(result) {
				if(result.success){
					$('#dataGrid').datagrid('reload');
				}else{
					alert(result.message);
				}
			}
		});
	}
}

function initType(id){
	$(id).combobox({
       valueField:'value',
       textField:'text',
       panelHeight:'auto',
       editable:false,
       data:[{
           'value':0,
           'text':'电类传感器',
           'selected':true
       },{
           'value':1,
           'text':'FBG温度'
       },{
           'value':2,
           'text':'FBG应力'
       },{
           'value':3,
           'text':'CCD位移'
       }]
   });
}